package com.valdemar.tellatale;

import java.util.HashMap;
import java.util.Map;

import com.stackmob.android.sdk.common.StackMobCommon;
import com.stackmob.sdk.api.StackMob;
import com.stackmob.sdk.api.StackMobQuery;
import com.stackmob.sdk.api.StackMobQueryWithField;
import com.stackmob.sdk.callback.StackMobCallback;
import com.stackmob.sdk.exception.StackMobException;
import com.stackmob.sdk.util.GeoPoint;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity_ extends Activity
{

	TextView txt = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		txt = (TextView) findViewById(R.id.textView1);

		Button btn = (Button) findViewById(R.id.btn_dummy);
		btn.setOnClickListener(call_activity);

	}

	OnClickListener dummy_click = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{

			Map<String, String> args = new HashMap<String, String>();
			args.put("username", "johndoe");
			args.put("password", "mypassword");
			StackMobCommon.getStackMobInstance().login(args,
					new StackMobCallback()
					{
						@Override
						public void success(String response)
						{

							txt.setText("oh yeahh!!");
							Toast.makeText(getBaseContext(), "oh yeahh!!",
									Toast.LENGTH_SHORT).show();
						}

						@Override
						public void failure(StackMobException e)
						{

							String ggg = "3";
							runOnUiThread(new Runnable()
							{
								public void run()
								{
									txt.setText("FUCK!!");

									Toast.makeText(getBaseContext(), "FUCK!!!",
											Toast.LENGTH_SHORT).show();
								}
							});

						}
					});

		}
	};

	OnClickListener nearTails = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{

			StackMobQueryWithField query = new StackMobQuery("tale").field("currentcoord").
					isNearWithinMi(new GeoPoint(-8.597488d, 41.149318d), 5d);

			StackMobCommon.getStackMobInstance().get(query, new StackMobCallback() {
				@Override
				public void success(String responseBody) {
					runOnUiThread(new Runnable()
					{
						public void run()
						{
							txt.setText(":)");

							Toast.makeText(getBaseContext(), "FUCK!!!",
									Toast.LENGTH_SHORT).show();
						}
					});
				}

				@Override
				public void failure(StackMobException e) {
					runOnUiThread(new Runnable()
					{
						public void run()
						{
							txt.setText("FUCK!!");

							Toast.makeText(getBaseContext(), "FUCK!!!",
									Toast.LENGTH_SHORT).show();
						}
					});
				}
			});

		}
	};
	
	
	OnClickListener tails = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
			getTales();
		}
	};
	
	
	
	
	private void getTales()
	{

		StackMob stackmob = StackMobCommon.getStackMobInstance();
		stackmob.get("tale", new StackMobCallback()
		{
			@Override
			public void success(String arg0)
			{
				final String d = arg0;

				runOnUiThread(new Runnable()
				{
					@Override
					public void run()
					{
						txt.setText(d);

					}
				});

			}

			@Override
			public void failure(StackMobException arg0)
			{
				runOnUiThread(new Runnable()
				{
					@Override
					public void run()
					{
						txt.setText("FUCK");

					}
				});

			}
		});
	}
	
	
	
	

	OnClickListener call_activity = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
			Intent intent = new Intent(getBaseContext(), TaleListActivity.class);
			startActivity(intent);
		}
	};

	//
	// OnClickListener custom_code_click = new OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	//
	//
	// Map<String, String> args = new HashMap<String, String>();
	// args.put("username", "johndoe");
	// args.put("password", "mypassword");
	//
	//
	// StackMobCommon.getStackMobInstance().get("hello_world", new
	// StackMobCallback() {
	// @Override public void success(String responseBody) {
	// //responseBody is "{ \"msg\": \"Hello, world!\" }"
	// }
	// @Override public void failure(StackMobException e) {
	// }
	// });
	// }
	// };
	//
	//

	OnClickListener register_click = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{

			Map<String, String> args = new HashMap<String, String>();
			args.put("username", "johndoe");
			args.put("password", "mypassword");
			args.put("email", "val@val.pt");

			// fields.put("views", new Integer(5));

			StackMob stackmob = StackMobCommon.getStackMobInstance();

			// schema name "blogentry" must be 3-25 alphanumeric characters,
			// lowercase
			stackmob.post("user", args, new StackMobCallback()
			{
				@Override
				public void success(String responseBody)
				{

					runOnUiThread(new Runnable()
					{
						public void run()
						{
							txt.setText("User Register");
						}
					});

				}

				@Override
				public void failure(StackMobException e)
				{

					final String txte = e.getMessage();

					runOnUiThread(new Runnable()
					{
						public void run()
						{
							txt.setText(txte);
						}
					});
				}
			});

		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
